<?php

/**
 * default actions.
 *
 * @package    Serag
 * @subpackage customer
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class customerActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $this->customers = Customer::loadAllCutomers();
    }

    public function executeAddCustomer(sfWebRequest $request) {
        $this->count = $this->constructArray($request);
        $this->form = new CustomerForm($this->count);

        if ($request->isMethod(sfRequest::POST)) {
            $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));

            if ($this->form->isValid()) {
                $values = $request->getParameter($this->form->getName());
                $result = CustomerTable::saveNewCustomer($this->count, $values);
                if ($result)
                    return $this->renderText('Done');
            }
        }
    }

    private function constructArray($request) {
        //array('activity' => 2,'owner'=>2,'consultant'=>1,'key_person'=>1,0=>2,1=>3);
        $array = array(
            'activity' => $request->getParameter('activity', 1),
            'owner' => $request->getParameter('owner', 1),
            'consultant' => $request->getParameter('consultant', 1),
            'key_person' => $request->getParameter('key_person', 1)
        );
        for ($i = 0; $i < $request->getParameter('contact_number', 1); $i++) {
            $array[$i] = $request->getParameter($i, 1);
        }
        return $array;
    }

    public function executeCustomerBrief(sfWebRequest $request) {
        $this->customer = Customer::getCutomerBrief($request->getParameter('customerId'));
    }

    public function executeLoadCustomers(sfWebRequest $request) {
        $this->customers = Customer::loadAllCutomers();
    }

    public function executeDeleteCustomer(sfWebRequest $request) {
        $id = $request->getParameter('customerId');
        Doctrine_Query::create()->update('Customer u')->set('u.is_deleted', '?', true)->where('u.id = ?', $id)->execute();
        return sfView::NONE;
    }

    public function executeShowCustomer(sfWebRequest $request) {
        $customer_id = $request->getParameter('customerId');
        $this->customer = Doctrine::getTable('Customer')->getDetails($customer_id);
        $this->contacts = Doctrine::getTable('Contact')->getDetails($customer_id);
        $this->balance = Doctrine::getTable('Transaction')->getBalance($customer_id);
        $this->activities = Doctrine::getTable('Activity')->getAll($customer_id);
    }

    public function executeEditCustomer(sfWebRequest $request) {
        $this->customer_id = $request->getParameter('customerId');
        $actions = $request->getParameter('actions',null);
        $customer = Doctrine::getTable('Customer')->getDetails($this->customer_id);
        if($actions == 'cancel')
            return $this->renderPartial('customer', array('customer'=>$customer));
        $this->form = new EditCustomerForm();
        $this->form->setDefaults($customer);

        if ($request->isMethod(sfRequest::POST)) {
            $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));

            if ($this->form->isValid()) {
            $values = $request->getParameter($this->form->getName());
                $editedCustomer = CustomerTable::saveEditedCustomer($this->customer_id, $values);
                if ($editedCustomer)
                    return $this->renderPartial('customer', array('customer'=>$editedCustomer, 'msg'=>'success'));
            }
        }
    }

    public function executeAddContact(sfWebRequest $request) {
        
    }

}
